package com.bdqn.wang.dao.impl;

import com.bdqn.wang.dao.OrderDao;
import com.bdqn.wang.pojo.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public List<Order> getAll() {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
            connection = getConnection();
            String sql = "select * from easybuy_order where shanchu=0";
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();

            //从结果集中取出数据，存储到集合当中
            //1.定义集合
            List<Order> orderList = new ArrayList<Order>();
            //2.遍历结果集
            while (resultSet.next()){
                Order order = new Order(
                        resultSet.getInt(1),
                        resultSet.getInt(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getDate(5),
                        resultSet.getString(6),
                        resultSet.getString(7)
                );
                orderList.add(order);
            }
            //5.返回集合
            return orderList;
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,ps,connection);
        }
        return null;
    }

    @Override
    public int add(Order order) {
        return 0;
    }

    @Override
    public int del(int id) {
        String sql="update easybuy_order set shanchu = 1 where id = ?";
        Object [] obj = {id};
        return executeUpdate(sql,obj);
    }

    @Override
    public int update(Order order) {
        String sql="update easybuy_order set userId=?,loginName=?,userAddress=?,createTime=?,cost=?,serialNumber=? where id = ?";
        Object [] obj = {
                order.getUserId(),
                order.getLoginName(),
                order.getUserAddress(),
                order.getCreateTime(),
                order.getCost(),
                order.getSerialNumber(),
                order.getId()
        };
        return executeUpdate(sql,obj);
    }

    @Override
    public Order getOrderByid(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = "select * from easybuy_order where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,id);
            resultSet = preparedStatement.executeQuery();
            Order order = new Order();
            if (resultSet.next()) {
                order.setId(resultSet.getInt(1));
                order.setUserId(resultSet.getInt(2));
                order.setLoginName(resultSet.getString(3));
                order.setUserAddress(resultSet.getString(4));
                order.setCreateTime(resultSet.getDate(5));
                order.setCost(resultSet.getString(6));
                order.setSerialNumber(resultSet.getString(7));
            }
            return order;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public Order getOrderXiang(int xiang) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = "select * from easybuy_user where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,xiang);
            resultSet = preparedStatement.executeQuery();
            Order order = new Order();
            if (resultSet.next()) {
                order.setId(resultSet.getInt(1));
                order.setUserId(resultSet.getInt(2));
                order.setLoginName(resultSet.getString(3));
                order.setUserAddress(resultSet.getString(4));
                order.setCreateTime(resultSet.getDate(5));
                order.setCost(resultSet.getString(6));
                order.setSerialNumber(resultSet.getString(7));
            }
            return order;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }

}
